package com.yiren.dbaa.module.practice_set.carmodel.view.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.CarModelEntity;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.module.practice_set.carmodel.contract.CarModelReleaseContract;
import com.yiren.dbaa.module.practice_set.carmodel.enums.AntennaLocationEnum;
import com.yiren.dbaa.module.practice_set.carmodel.enums.AntennaTypeEnum;
import com.yiren.dbaa.module.practice_set.carmodel.presenter.CarModelReleasePresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/12/6 15:07
 * @description: 车模发布
 */
public class CarModelReleaseActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_carmodel_name)
    TextView tvCarmodelName;
    @BindView(R.id.tv_antenna)
    TextView tvAntenna;
    @BindView(R.id.tv_location)
    TextView tvLocation;
    @BindView(R.id.tv_update_detail)
    TextView tvUpdateDetail;
    @BindView(R.id.ll_updatedetail_tv)
    LinearLayout llUpdatedetailTv;
    @BindView(R.id.et_update_detail)
    EditText etUpdateDetail;
    @BindView(R.id.ll_updatedetail_et)
    LinearLayout llUpdatedetailEt;
    @BindView(R.id.sp_update_way)
    Spinner spUpdateWay;
    @BindView(R.id.ll_update_way)
    LinearLayout llUpdateWay;
    @BindView(R.id.sp_update_user)
    Spinner spUpdateUser;
    @BindView(R.id.tv_using_range_bt)
    TextView tvUsingRangeBt;
    @BindView(R.id.ll_update_user)
    LinearLayout llUpdateUser;
    @BindView(R.id.ll_car_model)
    LinearLayout llCarModel;

    private CarModelEntity carModel;
    private UserParamConfigBean userParamConfig;
    private CarModelReleaseContract.Presenter releasePresenter;

    private int updateMode = 1;//更新方式 1:提示更新 2:强制更新

    private List<String> updateModes = new ArrayList<>();//更新方式
    private List<String> updateUsers = new ArrayList<>();//更新用户

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_release_carmodel);
        ButterKnife.bind(this);
        tvTitle.setText("车模数据发布");
        carModel = (CarModelEntity) getIntent().getSerializableExtra("carModel");
        init();
    }

    private void init() {
        releasePresenter = new CarModelReleasePresenter(this);
        userParamConfig = LoadConfigApi.loadUserParamConfig(this);
        if (carModel != null) {
            tvCarmodelName.setText(carModel.getName());
            tvAntenna.setText(AntennaTypeEnum.getValue(carModel.getAntennaType()));
            tvLocation.setText(AntennaLocationEnum.getValue(carModel.getPositioned()));
            if (carModel.getCarModelVer() == 0) {
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) llCarModel.getLayoutParams();
                lp.topMargin = 80;
                llCarModel.setLayoutParams(lp);
                tvUpdateDetail.setText("首次发布");
                llUpdateUser.setVisibility(View.GONE);
                llUpdateWay.setVisibility(View.GONE);
                llUpdatedetailTv.setVisibility(View.VISIBLE);
                llUpdatedetailEt.setVisibility(View.GONE);
            } else {
                llUpdateUser.setVisibility(View.VISIBLE);
                llUpdateWay.setVisibility(View.VISIBLE);
                llUpdatedetailTv.setVisibility(View.GONE);
                llUpdatedetailEt.setVisibility(View.VISIBLE);
            }
        }
        //数据源
        updateModes.add("提示更新");
        updateModes.add("强制更新");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item_spinner, updateModes);
        adapter.setDropDownViewResource(R.layout.item_spinner_dropdown);
        spUpdateWay.setAdapter(adapter);
        spUpdateWay.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                updateMode = position + 1;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        updateUsers.add("个别用户");
        updateUsers.add("全部用户");
        adapter = new ArrayAdapter<String>(this, R.layout.item_spinner, updateUsers);
        adapter.setDropDownViewResource(R.layout.item_spinner_dropdown);
        spUpdateUser.setAdapter(adapter);
        if (carModel.getUseRange() == 2) {
            spUpdateUser.setSelection(1);
        } else {
            spUpdateUser.setSelection(0);
        }
        spUpdateUser.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                carModel.setUseRange(position + 1);
                if (position == 0) {
                    tvUsingRangeBt.setVisibility(View.VISIBLE);
                } else {
                    tvUsingRangeBt.setVisibility(View.GONE);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @OnClick({R.id.iv_back, R.id.tv_release_carmodel})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_release_carmodel:
                releasePresenter.releaseCarModel(carModel, userParamConfig.getUserInfo().getId().toString(), etUpdateDetail.getText().toString(), updateMode);
                break;
        }
    }

}
